package models;
 
import java.util.*;

import javax.persistence.*;

import controllers.Admin;
import play.db.jpa.*;
import play.data.validation.*;
 
@Entity
public class User extends Model {
 
    @Email
    @Required
    public String email;
    
    @Required
    public String password;
    
    public String name;
    public boolean isAdmin;
    
	@OneToMany(mappedBy="author")
	public List<Event> createdEvents;
	
	@ManyToMany(mappedBy="participants")
	public List<Event> participation;
	
	public User(String name, String email,String password) {
		super();
		this.name = name;
		this.email = email;
		this.password = password;
		this.createdEvents = new ArrayList<models.Event>();
		this.participation = new ArrayList<models.Event>();
	}
	
	
	public User() {
		// TODO Auto-generated constructor stub
		this.createdEvents = new ArrayList<models.Event>();
		this.participation = new ArrayList<models.Event>();
	}
	


	
	// when a user signs in
	public static User connect(String email,String password) {
	    return find("byEmailAndPassword", email, password).first();
		//return find("byEmail").first();
	}
	// create event
	public static void create(Event event)
	{
		event.save();
	}
	// delete an event
	public static void delete(Event event)
	{
		event.delete();
	}
	
	// modifiy an event 
	public static void modify(Event e){
		
	}
	
	/**
	 * 
	 * @param e utilisateur a comparer 
	 * @return true si les noms et les emails des utilisateurs sont les memes
	 * On doit peut integrer les mots de passe plus tard
	 */
	
	public boolean equals(User e){
		
		return (this.email == e.email || this.name == e.name);
	}
	
	public void setParticipation(Event e){
		//this.participation.add(e);
	}
	
	public void setCreatedEvents(Event e){
		this.createdEvents.add(e);
	}
}



